package toubiao.utils.old;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.PropertyProjection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import toubiao.domain.excel.ProjectGrid4Excel;
import toubiao.domain.po.business.PoContract;
import toubiao.domain.po.business.PoPartA;
import toubiao.domain.po.business.PoProject;
import toubiao.domain.po.business.PoProjectSn;
import toubiao.utils.ProConstant;
import utils.UtilCollection;
import utils.UtilString;

public class ProUtilPoExcelConvert {

	static private Logger logger=LoggerFactory.getLogger(ProUtilPoExcelConvert.class);
	
	static public <S,T> T convertRoute(Class<S> sourceClass,Class<T> targetClass,S s) throws InstantiationException, IllegalAccessException{
		if(s==null){
			return null;
		}
		T t=null;
		if(sourceClass.getSimpleName().equalsIgnoreCase("PoProject")){
			PoProject sourceBeanFlag=new PoProject();
			if(targetClass.getSimpleName().equalsIgnoreCase("ExcelProjectGrid")){
				ProjectGrid4Excel targetBeanFlag=new ProjectGrid4Excel();
				t=(T) getVo(sourceBeanFlag,targetBeanFlag,(PoProject) s);
				return t;
			}
		}
		return t;
	}
	
	static public <S,T> List<T> getList(Class<S> sourceClass,Class<T> targetClass,List<S> sList) throws InstantiationException, IllegalAccessException{
		List<T> tList=new ArrayList<>();
		for(S s:sList){
			T t=convertRoute(sourceClass, targetClass, s);
			tList.add(t);
		}
		return tList;
	}
	
	static public ProjectGrid4Excel getVo(PoProject sourceTypeFlag,ProjectGrid4Excel targetTypeFlag,PoProject project){
		if(project==null){
			return null;
		}
		ProjectGrid4Excel excelProject=new ProjectGrid4Excel();
		BeanUtils.copyProperties(project, excelProject);
		
		String contractIds="";
		String contractSns="";
		Double contractAmount=0D;
		
		Set<PoProjectSn> snSet=project.getSnSet();
		/*logger.info("id = "+project.getId());
		logger.info(snSet.size()+"");*/
		String sns="";
		
		List<Integer> snList=new ArrayList<>();
		if(!UtilCollection.isNullOrEmpty(snSet)){
			for(PoProjectSn sn:snSet){
				Integer integer=new Integer(-1);
				
				if(sn==null || sn.getSnInt()==null){
				}else {
					integer=sn.getSnInt();
				}
				snList.add(integer);
				logger.info(sns);
			}
			sns=UtilCollection.numberList2String(snList, "asc", ProConstant.STRING_DIVIDOR);
			logger.info(sns);
		}
		excelProject.setSns(sns);
		
		Set<PoContract> contractSet=project.getContractSet();
		if(!UtilCollection.isNullOrEmpty(contractSet)){
			for(PoContract contract:contractSet){
				contractIds=contract.getId()+ProConstant.STRING_DIVIDOR;
				if(contract.getSn()==null){
					contractSns+=ProConstant.STRING_DIVIDOR;
				}else {
					contractSns=contract.getSn()+ProConstant.STRING_DIVIDOR;
				}
				contractAmount+=contract.getAmount();
			}
			contractIds=UtilString.deleteTail(contractIds, ProConstant.STRING_DIVIDOR.length());
			contractSns=UtilString.deleteTail(contractSns, ProConstant.STRING_DIVIDOR.length());
			
			excelProject.setContractSns(contractSns);
			excelProject.setContractAmount(contractAmount);
		}
		
		PoPartA partA=project.getPartA();
		if(partA!=null){
			excelProject.setPartAName(partA.getName());
			excelProject.setPartAType(partA.getType());
		}
		return excelProject;
	}
	
	static public ProjectGrid4Excel getVo(PoProject project){
		if(project==null){
			return null;
		}
		ProjectGrid4Excel ExcProject=new ProjectGrid4Excel();
		BeanUtils.copyProperties(project, ExcProject);
		
		String contractIds="";
		String contractSns="";
		Double contractAmount=0D;
		
		Set<PoProjectSn> snSet=project.getSnSet();
		/*logger.info("id = "+project.getId());
		logger.info(snSet.size()+"");*/
		String sns="";
		
		List<Integer> snList=new ArrayList<>();
		if(!UtilCollection.isNullOrEmpty(snSet)){
			for(PoProjectSn sn:snSet){
				Integer integer=new Integer(-1);
				
				if(sn==null || sn.getSnInt()==null){
				}else {
					integer=sn.getSnInt();
				}
				snList.add(integer);
				logger.info(sns);
			}
			sns=UtilCollection.numberList2String(snList, "asc", ProConstant.STRING_DIVIDOR);
			logger.info(sns);
		}
		ExcProject.setSns(sns);
		
		Set<PoContract> contractSet=project.getContractSet();
		if(!UtilCollection.isNullOrEmpty(contractSet)){
			for(PoContract contract:contractSet){
				contractIds=contract.getId()+ProConstant.STRING_DIVIDOR;
				if(contract.getSn()==null){
					contractSns+=ProConstant.STRING_DIVIDOR;
				}else {
					contractSns=contract.getSn()+ProConstant.STRING_DIVIDOR;
				}
				contractAmount+=contract.getAmount();
			}
			contractIds=UtilString.deleteTail(contractIds, ProConstant.STRING_DIVIDOR.length());
			contractSns=UtilString.deleteTail(contractSns, ProConstant.STRING_DIVIDOR.length());
			
			ExcProject.setContractSns(contractSns);
			ExcProject.setContractAmount(contractAmount);
		}
		
		PoPartA partA=project.getPartA();
		if(partA!=null){
			ExcProject.setPartAName(partA.getName());
			ExcProject.setPartAType(partA.getType());
		}

		return ExcProject;
	}
	
	static public List<ProjectGrid4Excel> getExcelProjectGridList(List<PoProject> projectList){
		if(projectList==null){
			return null;
		}
		List<ProjectGrid4Excel> excProjectList=new ArrayList<>();
		for(PoProject poProject:projectList){
			ProjectGrid4Excel excProject=getVo(poProject);
			excProjectList.add(excProject);
		}
		return excProjectList;
	}
	
	
}
